﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceKnownType(typeof(Task))]
[ServiceKnownType(typeof(Task_News))]
[ServiceKnownType(typeof(Task_Broadcast))]
public class Service
{
    private static SqlConnection conn = null;

    [OperationContract]
    public List<Task> GetTasks()
    {
        List<Task> tasks = new List<Task>();
        if (conn == null)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["TheAgeDatabaseConnectionString"].ConnectionString;
            conn = new SqlConnection(connectionString);
        }

        try
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            string domain_name = HttpContext.Current.User.Identity.Name;

            string SQL = "SELECT * FROM [VIEW_TaskNews] WHERE operator_dname=@UserName AND status=0";
            SqlCommand cmd = new SqlCommand(SQL, conn);

            cmd.Parameters.Add("@UserName", SqlDbType.VarChar);
            cmd.Parameters["@UserName"].Value = domain_name;

            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Task_News task = new Task_News();
                task.start_time = Convert.ToDateTime(reader["start_time"]);
                task.end_time = Convert.ToDateTime(reader["end_time"]);
                task.ID = Convert.ToInt32(reader["ID"]);
                task.date = Convert.ToDateTime(reader["date"]);
                task.coordinator = reader["director"].ToString();
                task.operater = reader["operator"].ToString();
                task.description = reader["description"].ToString();
                task.level = (Task.ETASKLEVEL)Convert.ToInt32(reader["level"]);
                task.everyday_task = Convert.ToBoolean(reader["everyday_task"]);
                task.title = reader["title"].ToString();
                task.link_addr = reader["link"].ToString();
                task.memo = reader["memo"].ToString();
                tasks.Add(task);
            }
            reader.Close();

            cmd.CommandText = "SELECT * FROM [VIEW_TaskBroadcast] WHERE operator_dname=@UserName AND status=0";
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Task_Broadcast task = new Task_Broadcast();
                task.start_time = Convert.ToDateTime(reader["start_time"]);
                task.end_time = Convert.ToDateTime(reader["end_time"]);
                task.ID = Convert.ToInt32(reader["ID"]);
                task.date = Convert.ToDateTime(reader["date"]);
                task.coordinator = reader["director"].ToString();
                task.operater = reader["operator"].ToString();
                task.description = reader["description"].ToString();
                task.level = (Task.ETASKLEVEL)Convert.ToInt32(reader["level"]);
                task.everyday_task = Convert.ToBoolean(reader["everyday_task"]);
                task.content = reader["content"].ToString();
                task.servers = reader["dest_servers"].ToString();
                task.memo = reader["memo"].ToString();
                tasks.Add(task);
            }
            reader.Close();
        }
        catch (System.Exception ex)
        {

        }
        finally
        {
            conn.Close();
        }

        return tasks;
    }

    [OperationContract]
    void NewsTaskFinish(int ID, DateTime date, string title, string link, string memo)
    {
        if (conn == null)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["TheAgeDatabaseConnectionString"].ConnectionString;
            conn = new SqlConnection(connectionString);
        }

        try
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();

            string SQL = "UPDATE VIEW_TaskNews SET status=1,title = @title, link=@link, memo=@memo WHERE ID=@ID AND date=@date AND operator_dname=@operator";

            SqlCommand cmd = new SqlCommand(SQL, conn);

            cmd.Parameters.Add("@title", SqlDbType.VarChar).Value = title;
            cmd.Parameters.Add("@link", SqlDbType.VarChar).Value = link;
            cmd.Parameters.Add("@memo", SqlDbType.VarChar).Value = memo;
            cmd.Parameters.Add("ID", SqlDbType.Int).Value = ID;
            cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = date;
            cmd.Parameters.Add("operator", SqlDbType.VarChar).Value = HttpContext.Current.User.Identity.Name;

            cmd.ExecuteNonQuery();
        }
        catch (System.Exception ex)
        {

        }
        finally
        {
            conn.Close();
        }
    }
}
